package me.stone.stanimeclient.core.common.extensions

import android.content.res.Resources
import android.util.DisplayMetrics
import android.util.TypedValue
import kotlin.math.roundToInt

private val metrics: DisplayMetrics
    get() = Resources.getSystem().displayMetrics

private fun apply(unit: Int, value: Float) = TypedValue.applyDimension(unit, value, metrics)

val Float.dip
    get() = apply(TypedValue.COMPLEX_UNIT_DIP, this)

val Float.sp
    get() = apply(TypedValue.COMPLEX_UNIT_SP, this)

val Float.px2dp
    get() = metrics.density.takeIf { it != 0f }?.let { this / it } ?: 0f

val Float.px2sp
    get() = metrics.scaledDensity.takeIf { it != 0f }?.let { this / it } ?: 0f